package cn.com.cbj.rabbitmq.consumer;


import cn.com.cbj.entity.EventInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
/**
 * 消息模型-消费者
 * @Author:debug (SteadyJack)
 **/
@Component
public class ModelConsumer {
    //定义日志
    private static final Logger log= LoggerFactory.getLogger(ModelConsumer.class);
    //JSON序列化和反序列化组件
    @Autowired
    public ObjectMapper objectMapper;
    /**
     * 监听并消费队列中的消息-fanoutExchange-one-这是第一条队列对应的消费者
     */
    @RabbitListener(queues = "${mq.fanout.queue.one.name}",containerFactory = "singleListenerContainer")
    public void consumeFanoutMsgOne(EventInfo eventInfo){
        try {
            //监听消费队列中的消息，并进行解析处理
            //EventInfo info=objectMapper.readValue(msg, EventInfo.class);
            log.info("消息模型fanoutExchange-one-消费者-监听消费到消息：{} ",eventInfo);
        }catch (Exception e){
            log.error("消息模型-消费者-发生异常：",e.fillInStackTrace());
        }
    }
    /**
     * 监听并消费队列中的消息-fanoutExchange-two-这是第二条队列对应的消费者
     */
    @RabbitListener(queues = "${mq.fanout.queue.two.name}",containerFactory = "singleListenerContainer")
    public void consumeFanoutMsgTwo(EventInfo eventInfo){
        try {
            //监听消费队列中的消息，并进行解析处理
            //EventInfo info=objectMapper.readValue(msg, EventInfo.class);
            log.info("消息模型fanoutExchange-two-消费者-监听消费到消息：{} ",eventInfo);
        }catch (Exception e){
            log.error("消息模型-消费者-发生异常：",e.fillInStackTrace());
        }
    }

    /** 这是第一个路由绑定的对应队列的消费者方法
     * 监听并消费队列中的消息-directExchange-one
     */
    @RabbitListener(queues = "${mq.direct.queue.one.name}",containerFactory =
            "singleListenerContainer")
    public void consumeDirectMsgOne(EventInfo eventInfo){
        try {
            //监听消费消息并进行JSON反序列化解析
//            EventInfo info=objectMapper.readValue(msg, EventInfo.class);
            //打印日志消息
            log.info("消息模型directExchange-one-消费者-监听消费到消息：{} ",eventInfo);
        }catch (Exception e){
            log.error("消息模型directExchange-one-消费者-监听消费发生异常：",e.
                    fillInStackTrace());
        }
    }
    /** 这是第二个路由绑定的对应队列的消费者方法
     * 监听并消费队列中的消息-directExchange-two
     */
    @RabbitListener(queues = "${mq.direct.queue.two.name}",containerFactory = "singleListenerContainer")
    public void consumeDirectMsgTwo(EventInfo eventInfo){
        try {
            //监听消费消息并进行JSON反序列化解析
//            EventInfo info=objectMapper.readValue(msg, EventInfo.class);
            //打印日志消息
            log.info("消息模型directExchange-two-消费者-监听消费到消息：{} ",eventInfo);
        }catch (Exception e){
            log.error("消息模型directExchange-two-消费者-监听消费发生异常：",e.
                    fillInStackTrace());
        }
    }

    /**
     * 监听并消费队列中的消息-topicExchange-*通配符
     */
    @RabbitListener(queues = "${mq.topic.queue.one.name}",containerFactory = "singleListenerContainer")
    public void consumeTopicMsgOne(String msg){
        try {
            //监听消费消息并进行解析
            //String message=new String(msg,"utf-8");
            log.info("消息模型topicExchange-*-消费者-监听消费到消息：{} ",msg);
        }catch (Exception e){
            log.error("消息模型topicExchange-*-消费者-监听消费发生异常：",e.fillInStackTrace());
        }
    }
    /**
     * 监听并消费队列中的消息-topicExchange-#通配符
     */
    @RabbitListener(queues = "${mq.topic.queue.two.name}",containerFactory = "singleListenerContainer")
    public void consumeTopicMsgTwo(String msg){
        try {
            //监听消费消息并进行解析
            //String message=new String(msg,"utf-8");
            log.info("消息模型topicExchange-#-消费者-监听消费到消息：{} ",msg);
        }catch (Exception e){
            log.error("消息模型topicExchange-#-消费者-监听消费发生异常：",e.fillInStackTrace());
        }
    }


}

